﻿using System;
using System.Collections.Specialized;
using System.Collections.Generic;
using System.Globalization;
using System.Linq;
using System.Linq.Dynamic;
using System.Text;
using IntraVision.Web.Toolkit;

namespace IntraVision.Web.Filter
{
    public class StringFilterCondition<TEntity> : IFilterCondition<TEntity>
        where TEntity: class, new()
    {
        string _Column;
        string _Caption;
        string _Value;
        int _Type = 0;

        public string Column
        {
            get { return _Column; }
            set { _Column = value; }
        }

        public bool Active
        {
            get { return _Type > 0 && !string.IsNullOrEmpty(_Value); }
        }

        string[] _Types = { HTMLHelper.Resource("Filter", "Contains", "содержит"), HTMLHelper.Resource("Filter", "NotContains", "не содержит"), HTMLHelper.Resource("Filter", "Equal", "равно"), HTMLHelper.Resource("Filter", "NotEqual", "не равно"), HTMLHelper.Resource("Filter", "StartsWith", "начинается с"), HTMLHelper.Resource("Filter", "EndsWith", "заканчивается на"), HTMLHelper.Resource("Filter", "Defined", "задано"), HTMLHelper.Resource("Filter", "Undefined", "не задано") };

        public StringFilterCondition(string column, string caption)
        {
            _Column = column;
            _Caption = caption;
        }

        #region IFilterCondition<TEntity> Members

        public void Init(NameValueCollection req)
        {
            _Value = req["flt" + _Column];
            if( !string.IsNullOrEmpty(req["ftt"+_Column]) ) 
                Int32.TryParse(req["ftt" + _Column], out _Type);
        }

        public IQueryable<TEntity> Filter(IQueryable<TEntity> list)
        {
            if (_Type == 7) return list.Where(String.Format(CultureInfo.InvariantCulture, "{0}.Length > 0", _Column));
            if (_Type == 8) return list.Where(String.Format(CultureInfo.InvariantCulture, "{0} == null || {0}.Length == 0", _Column));

            if (String.IsNullOrEmpty(_Value))
                return list;

            switch (_Type)
            {
                case 1:
                    return list.Where(String.Format(CultureInfo.InvariantCulture, "{0}.Contains(@0)", _Column), _Value);
                case 2:
                    return list.Where(String.Format(CultureInfo.InvariantCulture, "!{0}.Contains(@0)", _Column), _Value);
                case 3:
                    return list.Where(String.Format(CultureInfo.InvariantCulture, "{0} == @0", _Column), _Value);
                case 4:
                    return list.Where(String.Format(CultureInfo.InvariantCulture, "{0} != @0", _Column), _Value);
                case 5:
                    return list.Where(String.Format(CultureInfo.InvariantCulture, "{0}.StartsWith(@0)", _Column), _Value);
                case 6:
                    return list.Where(String.Format(CultureInfo.InvariantCulture, "{0}.EndsWith(@0)", _Column), _Value);
                default:
                    return list;
            }
        }

        public string Render()
        {
            StringBuilder sb = new StringBuilder();
            sb.Append(string.Format(CultureInfo.InvariantCulture, @"<div class=""filterfield""><table width=""100%""><tr><td class=""tdlabel""><label for=""flt{0}"">{1}</label></td>", _Column, _Caption));
            sb.Append(string.Format(CultureInfo.InvariantCulture, @"<td class=""tdcondition""><select class=""flttype ftttext"" id=""ftt{0}"" name=""ftt{0}""><option value=""0"" />", _Column));
            for (int i = 0; i < _Types.Length; i++)
                sb.Append(string.Format(CultureInfo.InvariantCulture, @"<option value=""{0}"" {2}>{1}</option>", i + 1, _Types[i], ((_Type == (i + 1)) ? " selected" : "")));
            sb.Append("</select></td>");
            sb.Append(string.Format(CultureInfo.InvariantCulture, @"<td class=""tdvalue""><input type=""text"" class=""flttext {2}"" id=""flt{0}"" name=""flt{0}"" value=""{1}""/></td></tr></table></div>", _Column, _Value, (_Type == 7 || _Type == 8) ? "hidden" : ""));
            return sb.ToString();
        }

        #endregion

        public override string ToString()
        {
            if (_Type <= 0) return string.Empty;
            return string.Format("ftt{0}={1}&flt{0}={2}", _Column, _Type, _Value);
        }
    }
}
